import { queryHotel, queryHotelInfo, updateHotelInfo, queryHotelRoom, updateHotelRoom, addHotelRoom } from '@/services/hotel';
import { message } from 'antd';

export default {
  namespace: 'hotel',

  state: {
    list: [],
    pagination:[],
    id: null,
    name: "",
    introduce: "",
    average_price: 0,
    headimgurl: "",
    province: "",
    city: "",
    district: "",
    amap_province: "",
    amap_city: "",
    amap_district: "",
    geographic:[],
    address: "",
    phone:'',
    facilities: null,
    lng: "",
    lat: "",
    created_at: "",
  },

  effects: {
    *fetchHotel({ payload }, { call, put }) {
      const response = yield call(queryHotel, payload);

      if(response.status === 'SUCCESS'){
        yield put({
          type: 'list',
          payload: response.data,
        });
      }else{
        message.error(response.failedMsg);
      }
    },

    *fetchHotelInfo({ payload, callback }, { call, put }) {
      const response = yield call(queryHotelInfo, payload);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'queryHotelInfo',
          payload: response.data,
        });
        console.log(222);
        callback()
      }else{
        message.error(response.failedMsg);
      }
    },

    *fetchHotelRoom({ payload }, { call, put }) {
      const response = yield call(queryHotelRoom, payload);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'list',
          payload: response.data,
        });
      }else{
        message.error(response.failedMsg);
      }
    },

    *fetchUpdateRoom({ payload }, { call, put }) {
      const response = yield call(updateHotelRoom, payload);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'updateRoom',
          payload: response.data,
        });
      }else{
        message.error(response.failedMsg);
      }
    },
    *fetchAddRoom({ payload }, { call, put }) {
      const response = yield call(addHotelRoom, payload);
      if(response.status === 'SUCCESS'){
        yield put({
          type: 'addRoom',
          payload: response.data,
        });
      }else{
        message.error(response.failedMsg);
      }
    },

    *fetchUpdateHotelInfo({ payload }, { call, put }) {
      const response = yield call(updateHotelInfo, payload);
      if(response.status === 'SUCCESS'){
        message.success('修改成功');
      }else{
        message.error(response.failedMsg);
      }
    },
    
  },

  reducers: {
    list(state, action) {
      return {
        ...state,
        list: action.payload.list,
        pagination:action.payload.pagination
      };
    },
    updateRoom(state, action){
      let newList = [];
      for(let i=0; i<state.list.length; i++){
        if(state.list[i].id === action.payload.id){
          action.payload.created_at = state.list[i].created_at;
          newList.push(action.payload);
        }else{
          newList.push(state.list[i]);
        }
      }
      return {
        ...state,
        list:newList
      }
    },

    addRoom(state, action){
      return {
        ...state,
        list:state.list.concat(action.payload)
      }
    },
    queryHotelInfo(state, action) {
      return {
        ...action.payload
      };
    },
  },
};
